// pages/address/address.js
const app = getApp();

Page({

	/**
	 * 页面的初始数据
	 */
	data: {
		switch_off: true,
		receiverName: "",
		mobile: "",
		addressDetail: "",
		postCode: "336600",
		isDisabled: false,
		isComplete: false,
		buttonTitle: "保存",
		region: ['请选择省', '请选择市', '请选择区'],
		biaoqian: [{
			'value': '家',
			id: 0
		}, {
			'value': '公司',
			id: 1
		}, {
			'value': '学校',
			id: 2
		}],
		biaoqianstutas: false,
		sexarr: [{
				'value': '先生',
				'id': 0,
				'checked': true
			},
			{
				'value': '女士',
				'id': 1,
				'checked': false
			},
		],
		sex: 0,
		label: '',
		region: ''
	},
	/* 选择标签 */
	biaoqian(e) {
		let id = e.currentTarget.dataset.id;
		let biaoqianstutas = this.data.biaoqianstutas = true;
		this.data.label = e.currentTarget.dataset.value;
		this.setData({
			labelid: id
		})
	},
	radio: function(e) {
		this.data.sex = e.currentTarget.dataset.id;
		this.setData({
			value: e.currentTarget.dataset.id
		})
	},
	bindRegionChange(e) {
		let that=this;
		console.log('picker发送选择改变，携带值为', e.detail.value);
		that.data.region = e.detail.value;
		//wx.setStorageSync('region',e.detail.value);
		console.log(that.data.region);
		this.setData({
			deliveryAddress: e.detail.value
		})
	},
	formSubmit(e) {
		let region = this.data.region;
		console.log(region);
		region=region[0]+region[1]+region[2];
		console.log(region);
		let label = this.data.label;
		let sex = this.data.sex;
		console.log(e.detail.value.linkMan);
		let addrInfo = {
			'linkMan': e.detail.value.linkMan,
			'phoneNumber': e.detail.value.phoneNumber,
			'deliveryAddress': region,
			'houseNumber': e.detail.value.houseNumber,
			'sex': sex,
			'label': label,
			'frequency': e.detail.value.frequency
		}
		console.log(addrInfo);
			
		let biaoqianstutas = this.data.biaoqianstutas; /* 判断是否选择家标签 */
		wx.setStorageSync('addrInfo', addrInfo)
		//console.log(receiverName,mobile,region_shi,address_total,switch_off)
		if (addrInfo.linkMan == "" || addrInfo.phoneNumber == "" || addrInfo.deliveryAddress[0] == "请选择省" || addrInfo.houseNumber ==
			"") {
			this.data.isComplete = false;
			wx.showModal({
				title: "提示",
				content: "请完善信息",
				showCancel: false
			});
		} else if (!/^[1][3,4,5,7,8]\d{9}$/.test(addrInfo.phoneNumber)) {
			wx.showModal({
				title: "提示",
				content: "手机号格式不规范",
				showCancel: false
			});
		} else if (biaoqianstutas == false) {
			wx.showModal({
				title: "提示",
				content: "请选择标签",
				showCancel: false
			});
		} else {
			this.data.isComplete = true;
			//wx.setStorageSync('addrInfo', addrInfo);
			this.updateAddr();
		}
		this.setData({
			isDisabled: false,
			isComplete: this.data.isComplete
			// buttonTitle: "修改"
		});

	},
	/* 提交地址 */
	updateAddr(e) {
		let userInfo = wx.getStorageSync('userInfo');
		let addrInfo = wx.getStorageSync('addrInfo');
		//console.log(addrInfo)
		let that = this;
		let sex = this.data.sex;
		let editaddres = wx.getStorageSync('editaddres');
		let base_url = app.globalData.baseurl;
		let _url = base_url + '/address/add';
		wx.request({
			url: _url,
			data: {
				'id': editaddres.id,
				'openId': userInfo.openId,
				'userId': userInfo.userId,
				'linkMan': addrInfo.linkMan,
				'sex': addrInfo.sex,
				'phoneNumber': addrInfo.phoneNumber,
				'deliveryAddress': addrInfo.deliveryAddress,
				'houseNumber': addrInfo.houseNumber,
				'label': addrInfo.label,
				'frequency': addrInfo.frequency
			},
			method: 'post',
			header: {
				'content-type': 'application/x-www-form-urlencoded', // 默认值
				'headerId': userInfo.openId,
				'headerValue': userInfo.headerValue
			},
			success: function(res) {
				console.log(res)
				return false;
				if (res.data.status = 200) {
					wx.redirectTo({
						url: '../myaddres/myaddres'
					})
				}

				// for (var i = 0; i < res.data.index_slide.length; i++) {
				//   var item = res.data.data[i];
				//   item.imageurl = item.prex + item.path;
				//   res.data.data[i] = item;
				// }
				// 	  			that.setData({
				// 	  				address_list:res.data.address_list,
				// 	  			})

			}
		})




		this.setData({
			isDisabled: false,
			isComplete: false,
			buttonTitle: "保存"
		});
	},
	/**
	 * 生命周期函数--监听页面加载
	 */
	onLoad: function(options) {
		this.editaddres();
	},
	editaddres(e) {
		let that = this;
		let editaddres = wx.getStorageSync('editaddres');
		that.data.region=editaddres.deliveryAddress;
		console.log(that.data.region);
		that.data.label=editaddres.label;
		that.data.sex=editaddres.sex;
		that.data.frequency=editaddres.frequency;
		that.data.region=editaddres.deliveryAddress;
		let sexarr = that.data.sexarr;
		let biaoqian = that.data.biaoqian;
		let labelid;
		/* 渲染先生女士 */
		for (let i = 0; i < sexarr.length; i++) {
			sexarr[i].checked = false;
			if (sexarr[i].id == editaddres.sex) {
				sexarr[i].checked = true;
			}
		}
		/* 渲染标签 */
		for (let i = 0; i < biaoqian.length; i++) {
			biaoqian[i].checked = false;
			if (biaoqian[i].value == editaddres.label) {
				labelid = biaoqian[i].id;
			}
		}
		console.log(editaddres);
		that.setData({
			deliveryAddress:editaddres.deliveryAddress,
			houseNumber:editaddres.houseNumber,
			linkMan:editaddres.linkMan,
			phoneNumber:editaddres.phoneNumber,
			frequency:editaddres.frequency,
			editaddress: editaddres,
			sexarr: sexarr,
			labelid: labelid
		})

	},
	/**
	 * 生命周期函数--监听页面初次渲染完成
	 */
	onReady: function() {

	},

	/**
	 * 生命周期函数--监听页面显示
	 */
	onShow: function() {

	},

	/**
	 * 生命周期函数--监听页面隐藏
	 */
	onHide: function() {

	},

	/**
	 * 生命周期函数--监听页面卸载
	 */
	onUnload: function() {

	},

	/**
	 * 页面相关事件处理函数--监听用户下拉动作
	 */
	onPullDownRefresh: function() {

	},

	/**
	 * 页面上拉触底事件的处理函数
	 */
	onReachBottom: function() {

	},

	/**
	 * 用户点击右上角分享
	 */
	onShareAppMessage: function() {

	}
})
